import { Toast } from 'vant'
import {interfaceUtils} from './interface.js'
let crypto = require('crypto');
// https://www.npmjs.com/package/jsencrypt?activeTab=versions
import JsEncrypt from "jsencrypt";
class Register extends interfaceUtils{
    static IsRequire=4
    static Name="register"
    static TimeStamp=new Date().getTime()
    constructor(phone,loginPassward,confirmPassword,code){
        super()
        this.phone=phone,
        this.loginPassward=loginPassward
        this.confirmPassword=confirmPassword,     
        this.code=code  
        this.timeStamp=new Date().getTime()      
    }
    _getParams(){
       const publicKey=Register.publicKey
       let rsa = new JsEncrypt();
       rsa.setPublicKey(publicKey);
       const { phone,loginPassward,code } =this
       const consumerFrom=Register.consumerFrom      
       const params=JSON.stringify( { phone,consumerFrom,loginPassward,code}) 
       const result= rsa.encrypt(params)  
        //    https://www.cnblogs.com/aeolian/p/12133598.html
       return encodeURIComponent(result)      
    }
    _getSign(){
       const key =Register.md5Key+""
       const {phone} =this 
       const consumerFrom=Register.consumerFrom+""
       const {timeStamp}=this
    //    https://blog.csdn.net/wail521/article/details/96430873       
        // const sign  = this._parmaSort({ key ,phone,consumerFrom ,timeStamp  }) 
        const sign  = phone +consumerFrom+timeStamp+key
        const result = crypto.createHash('md5').update(sign).digest("hex")    
        return result 
    }

    _getData(){
      
        const params=this._getParams()
        const sign=this._getSign()
        const {phone} = this
        const consumerFrom=Register.consumerFrom
        const {timeStamp}=this

        let formData = new FormData();
        formData.append("params",params);
        formData.append("sign",sign);
        formData.append("phone",phone);
        formData.append("consumerFrom",consumerFrom);
        formData.append("timeStamp",timeStamp);



        return  formData


    }

      /**
     * 
     * @param {keyObj} 将对象排序 变成 字符串
     *  例如 a=1&b=2 
     */
    _parmaSort(keyObj){
        let keyArr = [];
      for (let key in keyObj) {
        keyArr.push(key);
      }
        let reaKryArr = keyArr.sort();
        let newArrStr = "";
        for (let i = 0; i < keyArr.length; i++) {
          newArrStr += reaKryArr[i] + "=" + keyObj[reaKryArr[i]] + "&";
        }
    
        let str = newArrStr.substr(0, newArrStr.length - 1);
        return str
    }

}

export function qualified(data){
    if(data == null || data ==""||data == undefined ){
        return false
    }else{
        return true
    }
}

export function validata(data,class_type){
    let qualify =false

    let set= Object.entries(data).slice(0,class_type.IsRequire)
    let ret= set.find((value,index)=>{
        return !qualified( value[1])
    })
    if( ret == undefined){  
        //符合要求   
        if( qualified(data.phone)  &&  qualified(data.loginPassward) && qualified(data.confirmPassword) && qualified(data.code) ){  
            switch (class_type.Name) {
                case 'register':
                    if((data.loginPassward+"").length<6 || (data.loginPassward+"").length>20){
                        Toast.fail({
                            message:"请输入6-20位密码"
                        })
                        qualify=false
                    }else{
                        if((data.confirmPassword+"") ==(data.loginPassward+"")){
                            qualify=true
                        }else{
                            qualify=false
                            Toast.fail({
                                message:"密码不一致"
                            })
                        }
                    }       
                    

                 break;               
            }        
              return qualify
            }else{             
                return qualify
            }     
    }else{
       Toast.fail(ret[0] +" is Null");
        return false   
    }   
 


    
}


export { Register }